package com.ncucoder.soms.controller;

import com.ncucoder.soms.dto.ResponseDTO;
import com.ncucoder.soms.entity.Product;
import com.ncucoder.soms.entity.User;
import com.ncucoder.soms.service.ProductService;
import com.ncucoder.soms.service.ProviderService;
import com.ncucoder.soms.service.UserService;
import com.ncucoder.soms.util.JwtTokenUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 *
 * 商品控制器
 *
 * 包括: 分页获取商品, 新增商品, 模糊查询商品, 多条件查询
 *
 *
 * @author <a href="https://www.ncucoder.com">hsowan</a>
 * @date 2019-07-10
 **/
@RestController
@RequestMapping("/api/v1")
public class ProductController {

    private final ProductService productService;

    private final UserService userService;

    public ProductController(ProductService productService, UserService userService) {
        this.productService = productService;
        this.userService = userService;
    }


    @PostMapping("/product")
    @ApiOperation("添加一件商品")
    public void addProduct(@RequestBody Product product, HttpServletRequest request, HttpServletResponse response) {
        String username = JwtTokenUtils.getUsername(request.getHeader("Authorization").replace(JwtTokenUtils.TOKEN_PREFIX, ""));

        User user = userService.getUserByUsername(username);

        if (user == null) {
            response.setStatus(ResponseDTO.UNAUTHORIZED);
            return;
        }

        // 对product做检查
        if (product.getName() == null
                || product.getUnit() == null
                || product.getPrice() == null
                || product.getPrice().compareTo(BigDecimal.valueOf(0)) < 0
                || product.getProviderId() == null) {
            response.setStatus(ResponseDTO.BAD_REQUEST);
            return;
        }

        product.setCreator(user.getId());

        productService.insertProduct(product);

    }

    @ApiOperation("分页获取商品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码, 默认值: 1", paramType = "query"),
            @ApiImplicitParam(name = "page_size", value = "每页展示的数目, 默认值: 10", paramType = "query")
    })
    @GetMapping("/products")
    public ResponseDTO listProducts(@RequestParam(defaultValue = "1", required = false) int page, @RequestParam(name = "page_size", defaultValue = "10", required = false) int pageSize) {
        int maxPageSize = 20;
        if (page <= 0 || pageSize <= 0 || pageSize > maxPageSize) {
            return new ResponseDTO(ResponseDTO.BAD_REQUEST, "参数范围不合法");
        }

        return new ResponseDTO(ResponseDTO.OK, productService.listProducts(page, pageSize));

    }

    @GetMapping("/products/count")
    @ApiOperation("获取商品总数")
    public ResponseDTO count() {

        int count = productService.countProducts();
        if (count >= 0) {
            return new ResponseDTO(ResponseDTO.OK, count);
        }
        return new ResponseDTO(ResponseDTO.SERVER_ERROR, ResponseDTO.SERVER_ERROR_MASSAGE);

    }

    @ApiOperation("删除商品")
    @ApiImplicitParam(name = "id", value = "商品id", required = true, dataTypeClass = Long.class, paramType = "query")
    @DeleteMapping("/product")
    public ResponseDTO delete(@RequestParam Long id){
        if (id == null || id <= 0) {
            return new ResponseDTO(ResponseDTO.BAD_REQUEST, "删除失败");
        }
        if (productService.deleteProductById(id) != 1) {
            return new ResponseDTO(ResponseDTO.SERVER_ERROR, ResponseDTO.SERVER_ERROR_MASSAGE);
        }
        return new ResponseDTO(ResponseDTO.OK, "删除成功");
    }


}
